'''
 ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
 │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
 └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
 ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
 │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
 ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
 │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
 ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
 │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
 ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
 │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
 ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
 │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
 └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘

Author: Faye Lee
Date: 2025-04-09 15:01:46
LastEditTime: 2025-04-15 17:53:14
LastEditors: Faye Lee
Description: 
FilePath: \pygame_alien\ship.py
Halo Baby,欢迎打开新世界的大门!
'''
import pygame
from pygame.sprite import Sprite
import image_scale as scale

class Ship(Sprite):
    def __init__(self,ai_settings,screen,width=0):
        """初始化飞船并设置其初始位置"""
        super(Ship,self).__init__()
        self.screen=screen
        self.ai_settings = ai_settings
        if width==0:
            self.width=ai_settings.ship_width
        else:
            self.width = width
        # 加载飞船图像并获取其外接矩形,按照宽100的等比例缩放
        self.image=scale.image_scale('image/robot.png',self.width)

        self.rect=self.image.get_rect()
        self.screen_rect=screen.get_rect()

        # 将每艘新飞船放在屏幕底部中央
        self.rect.centerx=self.screen_rect.centerx
        self.rect.bottom=self.screen_rect.bottom

        # 在飞船的属性center中存储小数值
        self.center = float(self.rect.centerx)
        self.centery = float(self.rect.bottom)

        # 移动标志
        self.moving_right=False
        self.moving_left=False
        self.moving_top=False
        self.moving_bottom=False

    def blitme(self):
        """在指定位置绘制飞船"""
        # print(self.rect)
        self.screen.blit(self.image,self.rect)
    
    def update(self):
        """根据移动标志调整飞船的位置"""
        # 更新飞船的center值而不是rect
        if self.moving_right and self.rect.right < self.screen_rect.right:
            # print("向右移动",self.center)
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left and self.rect.left >0:
            # print("向左移动",self.center)
            self.center -= self.ai_settings.ship_speed_factor
        if self.moving_top and self.rect.top >0:
            # print("向上移动",self.centery)
            self.centery -= self.ai_settings.ship_speed_factor
        if self.moving_bottom and self.rect.bottom < self.screen_rect.bottom:
            # print("向下移动",self.centery)
            self.centery += self.ai_settings.ship_speed_factor

        # 根据self.center更新rect对象
        self.rect.centerx = self.center
        self.rect.bottom = self.centery
    def center_ship(self):
        """让飞船在屏幕上居中"""
        self.center = self.screen_rect.centerx
        self.centery = self.screen_rect.bottom

    




